var app = new Vue({
    el: "#app",
    data: {
        form: {
            ywx: "",        //英文姓
            ywm: "",        //英文名
            zwxm: "",         //中文姓名
            jnlxdh: "",        //境内联系电话，申请人电话
            gjdq: "ABW",          //国家地区
            csrq: "2000-01-01",
            csd: "ABW",
            xb: "1",
            jnjzdxxdz: "",     //境内居住地详细地址(在华住址)
            dbr: "",            //代办人：联系人姓名
            dbrlxdh: "",       //代办人联系电话：联系人电话
            xczjzl: "14",          //现持证件种类：护照类型
            xczjhm: "",          //现持证件号码：护照号码
            xczjyxqz: "",          //现持证件有效期至：护照有效期至
            xcqzzl: "",
            xcqzhm: "",          //现持签证号码
            xcqzyxqz: "",        //现持签证有效期至
            xctlyxqz: "",         //现持停留有效期至
            sqlb: "",
            sqqzzl: "",
            jlxksy: "",
            bzlx: "",
            qzyxqz: null
        },

        csdOptions: csdOptions,
        gjdqKeyword: "",
        gjdqDisplay: false,
        csdKeyword: "",
        csdDisplay: false

    },
    computed: {
        ywx: {
            set: function (newValue) {
                this.form.ywx = newValue.toUpperCase();
            },
            get: function () {
                return this.form.ywx;
            }
        },
        ywm: {
            set: function (newValue) {
                this.form.ywm = newValue.toUpperCase();
            },
            get: function () {
                return this.form.ywm;
            }
        },
        xczjhm: {
            set: function (newValue) {
                this.form.xczjhm = newValue.toUpperCase();
            },
            get: function () {
                return this.form.xczjhm;
            }
        },
        formJson: function () {
            return JSON.stringify(this.form);
        },
        xcqzDisplay: function () {
            if (this.form.xcqzzl == 'V' || this.form.xcqzzl == 'M' || this.form.xcqzzl == '' || this.form.xcqzzl == 't') {
                return true;
            }
            else {
                return false;
            }
        },
        sqqzDisplay: function () {
            if (this.form.sqlb == '202') {
                return false;
            }
            else {
                return true;
            }
        },
        sqtlDisplay: function () {
            if (this.form.sqlb == '207' || this.form.sqlb == '205') {
                return false;
            }
            else {
                return true;
            }
        },

        sqlb: {
            set: function (newValue) {
                this.form.sqlb = newValue;
                this.form.sqqzzl = "";
                this.form.jlxksy = "";
            },
            get: function () {
                return this.form.sqlb;
            }
        },
        jlxksyOptions: function () {
            if (this.form.sqlb == '207') {
                return [{key: "31", value: "免签"}, {key: "32", value: "船员"}, {key: "33", value: "退籍"}, {
                    key: "34",
                    value: "人道主义"
                }, {key: "99", value: "其他"}]
            }
            if (this.form.sqlb == '205') {
                return [{key: "03", value: "工作"}, {key: "07", value: "学习"}, {key: "05", value: "记者"}, {
                    key: "14",
                    value: "团聚"
                }, {key: "15", value: "私人事务"}]
            }
            return [];
        },
        bzlbOptions: function () {
            if (this.form.sqlb == '202') {
                this.form.bzlx = 'W2';
                return [{key: "W2", value: "延期"}, {key: "W3", value: "换发"}, {key: "W4", value: "补发"}];
            }
            if (this.form.sqlb == '205') {
                this.form.bzlx = 'W1';
                return [{key: "W1", value: "申请"}, {key: "W2", value: "延期"}, {key: "W3", value: "换发"}, {
                    key: "W4",
                    value: "补发"
                }];
            }
            if (this.form.sqlb == '207') {
                this.form.bzlx = 'W1';
                return [{key: "W1", value: "申请"}, {key: "W3", value: "换发"}, {key: "W4", value: "补发"}];
            }
        },
        jlxksyTitle: function () {
            if (this.form.sqlb == '205') {
                return '申请居留事由';
            }
            else {
                return '申请停留事由';
            }
        },
        sqlbOptions: function () {
            if (this.form.xcqzzl == 'M' || this.form.xcqzzl == 'V') {
                this.form.sqlb = '207';
                return [{key: "207", value: "停留证件"}];
            }
            else {
                return [{key: "202", value: "普通签证"}, {key: "207", value: "停留证件"}, {key: "205", value: "居留许可"}];
            }

        },
        sqqzzlOptions:function(){
            if(this.form.bzlx=='W2')
            {
                return [{key: "Q2", value: "团聚"},{key: "J2", value: "记者"},{key: "S2", value: "私人事务"},{key: "X2", value: "学习"}];
            }
            else {
                return [{key: "F", value: "访问"},{key: "L", value: "旅游"},{key: "M", value: "贸易"},{key: "Q2", value: "团聚"},{key: "J2", value: "记者"},{key: "S2", value: "私人事务"},{key: "X2", value: "学习"},{key: "R", value: "人才"},{key: "G", value: "过境"},{key: "C", value: "乘务"},{key: "3T", value: "团签"}];
            }
        },

        gjdqOptionList: function () {
            if (this.gjdqKeyword == '') {
                return gjdqOptions;
            }
            var tempOptions = [];
            for (i = 0; i < gjdqOptions.length; i++) {
                if (gjdqOptions[i].key.indexOf(this.gjdqKeyword.toUpperCase()) >= 0 || gjdqOptions[i].value.indexOf(this.gjdqKeyword.toUpperCase()) >= 0) {
                    tempOptions.push(gjdqOptions[i]);

                }
            }
            return tempOptions;
        },
        gjdqName: function () {
            var name = getValue(gjdqOptions, this.form.gjdq);
            return this.form.gjdq + " - " + name;
        },
        csdOptionList: function () {
            if (this.csdKeyword == '') {
                return csdOptions;
            }
            var tempOptions = [];
            for (i = 0; i < csdOptions.length; i++) {
                if (csdOptions[i].key.indexOf(this.csdKeyword.toUpperCase()) >= 0 || csdOptions[i].value.indexOf(this.csdKeyword.toUpperCase()) >= 0) {
                    tempOptions.push(csdOptions[i]);

                }
            }
            return tempOptions;
        },
        csdName: function () {
            var name = getValue(csdOptions, this.form.csd);
            return this.form.csd + " - " + name;
        }
    },
    methods: {
        submit: function (e) {
            if (this.validateInput()) {
                $("#wgrtbForm").submit();
            }
        },
        validateInput: function () {


            if (!checkNull(this.form.ywx, this.form.ywm)) {
                showDialog("请输入正确的姓名!");
                return false;
            }
            if (!checkPhone(this.form.jnlxdh)) {
                showDialog("请输入正确的申请人电话!");
                return false;
            }
            if (!checkNull(this.form.jnjzdxxdz)) {
                showDialog("请输入正确的在华住址!");
                return false;
            }
            if (!checkNull(this.form.dbr)) {
                showDialog("请输入正确的联系人姓名!");
                return false;
            }

            if (!checkPhone(this.form.dbrlxdh)) {
                showDialog("请输入正确的联系人电话!");
                return false;
            }
            if (!checkNull(this.form.xczjhm)) {
                showDialog("请输入正确的护照号码!");
                return false;
            }
            if (!checkNull(this.form.xczjyxqz)) {
                showDialog("请选择正确的护照有效期!");
                return false;
            }
            if (!checkNull(this.form.xcqzzl)) {
                showDialog("请选择正确的现持有效签证证件种类!");
                return false;
            }
            if (this.form.xcqzzl != null && this.form.xcqzzl != 'M' && this.form.xcqzzl != 'V') {
                if (!checkNull(this.form.xcqzhm)) {
                    showDialog("请输入正确的现持签证证件号码!");
                    return false;
                }
                if (!checkNull(this.form.xcqzyxqz)) {
                    showDialog("请选择正确的现持签证有效期!");
                    return false;
                }
            }
            if (!checkNull(this.form.sqlb)) {
                showDialog("请选择您要申请的证件类别!");
                return false;
            }
            if (!checkNull(this.form.bzlx)) {
                showDialog("请选择您要申请的办证类别!");
                return false;
            }


            if (this.form.sqlb == '202') {
                if (!checkNull(this.form.sqqzzl)) {
                    showDialog("请选择您要申请的签证类别!");
                    return false;
                }
            }
            else if (this.form.sqlb == '205') {
                if (!checkNull(this.form.jlxksy)) {
                    showDialog("请选择您要申请居留的事由!");
                    return false;
                }
            }
            else {
                if (!checkNull(this.form.jlxksy)) {
                    showDialog("请选择您要申请停留的事由!");
                    return false;
                }
            }

            if (!checkNull(this.form.qzyxqz)) {
                showDialog("请选择您要申请申请停留的期限!");
                return false;
            }

            var hzyxq = new Date(this.form.xczjyxqz);
            var now = new Date();
            var sqqzyxq = new Date(this.form.qzyxqz);

            if (this.form.sqlb == '202') {
                if (sqqzyxq.getTime() > hzyxq.getTime()) {
                    showDialog("申请的停留期限不能超过所持护照有效期!");
                    return false;
                }

                if ((sqqzyxq.getTime() - now.getTime()) / 86400000 > 180) {
                    showDialog("申请普通签证的停留期限不能超过180天!");
                    return false;
                }
            }
            else if (this.form.sqlb == '205') {
                if (sqqzyxq.getTime() > hzyxq.getTime()) {
                    showDialog("申请的停留期限不能超过所持护照有效期!");
                    return false;
                }

                if ((sqqzyxq.getTime() - now.getTime()) / 86400000 > 1825) {
                    showDialog("申请居留许可的停留期限不能超过5年!");
                    return false;
                }
            }
            else {
                if (sqqzyxq.getTime() > hzyxq.getTime()) {
                    showDialog("申请的停留期限不能超过所持护照有效期!");
                    return false;
                }

                if ((sqqzyxq.getTime() - now.getTime()) / 86400000 > 180) {
                    showDialog("申请停留证件的停留期限不能超过180天!");
                    return false;
                }
            }

            return true;
        },
        clickGjdq: function (_key) {
            return "javascript:pickGjdq('" + _key + "');";
        },
        clickCsd: function (_key) {
            return "javascript:pickCsd('" + _key + "');";
        }
    }
});

function pickGjdq(_key) {
    app.gjdqDisplay = false;
    app.form.gjdq = _key;
    app.gjdqKeyword = '';
}

function showPickGjdq() {
    app.gjdqKeyword = '';
    app.gjdqDisplay = true;
}

function hidePickGjdq() {
    app.gjdqKeyword = '';
    app.gjdqDisplay = false;
}


function pickCsd(_key) {
    app.csdDisplay = false;
    app.form.csd = _key;
    app.csdKeyword = '';
}

function showPickCsd() {
    app.csdKeyword = '';
    app.csdDisplay = true;
}

function hidePickCsd() {
    app.csdKeyword = '';
    app.csdDisplay = false;
}